package com.yun.algorithmproblem.leetcode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Leetcode78 {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        result.add(new ArrayList<>());
        for (int num : nums) {
            List<List<Integer>> currList = new ArrayList<>(result);
            for (List<Integer> list : result) {
                ArrayList<Integer> curr = new ArrayList<>(list);
                curr.add(num);
                currList.add(curr);
            }
            result = currList;
        }
        return result;
    }

    /*
        result存放结果
        stack栈用于回溯
        curr记录第几个数字
        对于每个数字有选择或者不选择
     */
    public List<List<Integer>> subsets1(int[] nums){
        List<List<Integer>> result=new ArrayList<>();
        dfs(nums,0,new LinkedList<>(),result);
        return result;
    }

    private void dfs(int[] nums,int curr,
                     LinkedList<Integer> stack,
                     List<List<Integer>> result){
        if(curr>= nums.length){
            result.add(new ArrayList<>(stack));
            return;
        }
        //选择
        stack.push(nums[curr]);
        dfs(nums,curr+1,stack,result);
        stack.pop();
        //不选择
        dfs(nums,curr+1,stack,result);
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1, 2, 3};
        Leetcode78 obj = new Leetcode78();
        obj.subsets1(nums);
    }
}
